from django.shortcuts import render

# Create your views here.

from .models import *


def index(request):
    articles = Article.objects.all()
    return render(request, 'blog/index.html', {'articles': articles})


def article_page(request, article_id):
    article = Article.objects.get(pk=article_id)
    return render(request, 'blog/detail.html', {'article': article})


def article_edit(request, article_id):
    if str(article_id) == '0':
        return render(request, 'blog/edit_page.html')
    article = Article.objects.get(pk=article_id)
    return render(request, 'blog/edit_page.html', {'article': article})


def edit_action(request):
    title = request.POST.get('title', 'TITLE')
    content = request.POST.get('content', 'CONTENT')
    article_id = request.POST.get('article_id', '0')
    if str(article_id) == '0':
        Article.objects.create(title=title, content=content)
        articles = Article.objects.all()
        return render(request, 'blog/index.html', {'articles': articles})
    else:
        article = Article.objects.get(pk=article_id)
        article.title = title
        article.content = content
        article.save()
        return render(request, 'blog/detail.html', {'article': article})


def add_author(request):
    # add
    # Author.objects.create(name='sss')
    # AuthorDetail.objects.create(sex=False, email='1197183452@qq.com', address='安徽', birthday='1988-01-06', author_id=1)
    # publisher = Publisher()
    # publisher.name = 'haha'
    # publisher.address = 'dd'
    # publisher.city = 'c'
    # publisher.country = 'd'
    # publisher.website = 'www.aa.com'
    # publisher.save()

    # update
    # publisher = Publisher.objects.get(id=1)
    # publisher.name = 'ttt'
    # publisher.save()

    # Publisher.objects.filter(id=1).update(city='bbb')

    # search
    # Publisher.objects.all()

    # delete
    Publisher.objects.filter(id=1).delete()
    return render(request, 'hello/author.html')
